﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using System.IO;

namespace Broker.View.Converter
{
    public class ImageConverter : IValueConverter
    {

        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            BitmapImage bitImage = null;

            if (value != null)
            {
                MemoryStream ms = new MemoryStream(value as byte[]);
                bitImage = new BitmapImage();
                bitImage.BeginInit();
                bitImage.StreamSource = ms;
                bitImage.EndInit();

            }
            return bitImage;

        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            BitmapImage img = value as BitmapImage;
            byte[] imageb = null;

            if (value != null)
            {
                Stream ms = new MemoryStream();

                ms = img.StreamSource;
                imageb = new byte[ms.Length];
                ms.Read(imageb, 0, imageb.Length);
            }
            return imageb;
        }

        #endregion
    }
}
